﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using MySql.Data;

namespace natureskitchen
{
    public partial class frmDailyNumbers : Form
    {
        int istaxincluded;

        public frmDailyNumbers()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDailyNumbers_Load(object sender, EventArgs e)
        {

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (rdoNo.Checked == true)
                {
                    istaxincluded = 0;
                }
                else if (rdoYes.Checked == true)
                {
                    istaxincluded = 1;
                }

                mysql.connectDB().Open();
                string selectquery = "SELECT * FROM DailyNumbers";
                string insertquery = "INSERT INTO DailyNumbers (Date, TotalCustomers, InsideOrders, ToGoOrders, TotalMoney, TaxPercent, IsTaxIncluded) VALUES ( ";
                insertquery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ", ";
                insertquery += "\'" + txtCustomerAmount.Text + "\'" + ", ";
                insertquery += "\'" + txtInsideOrders.Text + "\'" + ", ";
                insertquery += "\'" + txtToGoOrders.Text + "\'" + ", ";
                insertquery += "\'" + txtTotalMoney.Text + "\'" + ", ";
                insertquery += "\'" + txtTaxAmount.Text + "\'" + ", ";
                insertquery += "\'" + istaxincluded + "\'" + ") ";

                MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(insertquery, mysql.connectDB());
                MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(selectquery, mysql.connectDB());
                MySql.Data.MySqlClient.MySqlDataReader dr = command.ExecuteReader();
                SmtpClient smtp = new SmtpClient("smtp.1and1.com");
                MailMessage message = new MailMessage("mailer@imaslog.com", "cheatcountry@gmail.com");
                message.Subject = "Nature's Kitchen Daily Numbers for " + DateTime.Now.ToLongDateString();
                message.Body = txtCustomerAmount.Text + " customers served today\r\n" + txtToGoOrders.Text + " customers ordered food to go\r\n" + txtInsideOrders.Text + " customers ordered food to eat inside\r\n" + txtTotalMoney.Text + " dollars were made today overall";
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("mailer@imaslog.com", "xESp3maheh3#a?EVuthu+EchafApr!?4&ruf=-juPhumamusta=r6fA?7@aP+&=!");
                smtp.Credentials = credentials;
                smtp.Send(message);
                MessageBox.Show("The info you have entered has been saved into the database");
                mysql.disconnectDB();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show("An error in the database has occurred:" + ex.Message);
            }
            catch (SmtpException smtpex)
            {
                MessageBox.Show("There was an smtp error:" + smtpex.Message);
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("You have to fill out all of the textboxes");
            }
        }
    }
}
